package com.itcast.tanhuaserver.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.tanhua.dubbo.server.api.QuanZiApi;
import com.itcast.tanhua.dubbo.server.domain.Comment;
import com.itcast.tanhua.dubbo.server.vo.PageInfo;
import com.itcast.tanhuaserver.domain.User;
import com.itcast.tanhuaserver.domain.UserInfo;
import com.itcast.tanhuaserver.domain.UserThreadLocal;
import com.itcast.tanhuaserver.service.CommentsService;
import com.itcast.tanhuaserver.service.UserInfoService;
import com.itcast.tanhuaserver.vo.Comments;
import com.itcast.tanhuaserver.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @PachageName: com.itcast.tanhuaserver.service.impl
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/1812:53
 * @Version 1.0
 */
@Service
public class CommentsServiceImpl implements CommentsService {
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {
        User user = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPages(0);
        pageResult.setCounts(0);
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize);
        List<Comment> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)){
            return pageResult; //没有查询到评论列表
        }

        //查询评论的所有用户
        List<Long> userIds = new ArrayList<>();
        for (Comment record : records) {
            if (!userIds.contains(record.getUserId())){
                userIds.add(record.getUserId());
            }
        }

        //查询所有评论的用户信息
        QueryWrapper<UserInfo> query = new QueryWrapper<>();
        query.in("user_id",userIds);
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(query);

        List<Comments> commentsList = new ArrayList<>();
        for (Comment record : records) {
            Comments comments = new Comments();
            comments.setContent(record.getContent());
            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));

            for (UserInfo userInfo : userInfoList) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    comments.setNickname(userInfo.getNickName());
                    comments.setAvatar(userInfo.getLogo());
                    break;
                }
            }

            String redisUserLikekey = "QUANZI_COMMENT_LIKE_USER_" + user.getId() + "_" + comments.getId();
            comments.setHasLiked(this.redisTemplate.hasKey(redisUserLikekey) ? 1 : 0); // 是否点赞（1是，0否）

            String redisLikeKey = "QUANZI_COMMENT_LIKE_" + comments.getId();
            String likeCountValue = this.redisTemplate.opsForValue().get(redisLikeKey);
            if (StringUtils.isNotEmpty(likeCountValue)){
                comments.setLikeCount(Integer.parseInt(likeCountValue)); // 点赞数
            }else{
                comments.setLikeCount(0); // 点赞数
            }

            commentsList.add(comments);

        }
        pageResult.setItems(commentsList);

        return pageResult;
    }

    @Override
    public boolean saveComments(String movementId, String content) {
        User user = UserThreadLocal.get();
        return this.quanZiApi.saveComment(user.getId().longValue(),movementId,2,content);
    }
}
